<?php

class BannerForm extends TPage
{
	const AR = "BannerRecord";
	const NO_IMAGE = "noimage.png";
	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->Master->pageHead->Title = $this->Application->Parameters["WEBSITE_TITLE"] . " Administrator";
		if (!$this->IsPostBack)
		{	
			$activeRecord = $this->getItem();
			if ($activeRecord && $activeRecord->ID > 0)
			{
				$this->lblHeader->Text = "Update banner: ".$activeRecord->BannerName;
				$this->txtName->Text = $activeRecord->BannerName;
				$this->txtURL->Text = $activeRecord->BannerURL;
				$this->radPublish->SelectedValue = $activeRecord->Status;
				$this->imgFull->Visible = true;
				$this->imgFull->ImageUrl = $this->Request->UrlManagerModule->UrlPrefix."/useruploads/images/banner/".$activeRecord->BannerPhoto;
				$this->imgThumb->Visible = true;
				$this->imgThumb->ImageUrl = $this->Request->UrlManagerModule->UrlPrefix."/useruploads/images/banner/thumbs/".$activeRecord->BannerThumb;
			}
			else
			{
				$this->lblHeader->Text = "Add new banner";
			}
		}
	}

	protected function getItem()
	{
		if ($this->Request->Contains("id"))
		{
			$activeRecord = Prado::createComponent(self::AR)->finder()->findByPk(TPropertyValue::ensureInteger($this->Request['id']));
			if($activeRecord === null)
			{
				$this->Notice->Type = AdminNoticeType::Error;
				$this->Notice->Text = $this->Application->getModule("message")->translate("ITEM_NOT_FOUND","Banner");
				$this->mainBox->Visible = false;
			}
			return $activeRecord;
		}
		else
		{
			return Prado::createComponent(self::AR);
		}
	}
		
	private function bindItem()
	{
		$activeRecord = $this->getItem();

		$hashImage = "";
		if($this->fuImage->HasFile) 
		{
			$hashImage = md5(uniqid(time())).'.'.strtolower(array_pop(explode('.',$this->fuImage->FileName)));
			$filePath = dirname($this->Request->ApplicationFilePath).DIRECTORY_SEPARATOR."useruploads".DIRECTORY_SEPARATOR."images".DIRECTORY_SEPARATOR."banner".DIRECTORY_SEPARATOR;
			if ($activeRecord->BannerPhoto != '' && $activeRecord->BannerPhoto != self::NO_IMAGE) 
			{
				// Delete old image
				try
				{
					chmod($filePath.$activeRecord->BannerPhoto, 0777);
					unlink($filePath.$activeRecord->BannerPhoto);
				}
				catch(TException $e){}
			}

			$this->fuImage->saveAs($filePath.$hashImage, true);
		}

		$hashThumb = "";
		if($this->fuThumb->HasFile) 
		{
			$hashThumb = md5(uniqid(time())).'.'.strtolower(array_pop(explode('.',$this->fuThumb->FileName)));
			$filePath = dirname($this->Request->ApplicationFilePath).DIRECTORY_SEPARATOR."useruploads".DIRECTORY_SEPARATOR."images".DIRECTORY_SEPARATOR."banner".DIRECTORY_SEPARATOR."thumbs".DIRECTORY_SEPARATOR;
			if ($activeRecord->BannerThumb != '' && $activeRecord->BannerThumb != self::NO_IMAGE) 
			{
				// Delete old thumbnail
				try
				{
					chmod($filePath.$activeRecord->BannerThumb, 0777);
					unlink($filePath.$activeRecord->BannerThumb);
				}
				catch(TException $e){}
			}

			$this->fuThumb->saveAs($filePath.$hashThumb, true);
		}		

		$activeRecord->BannerName = $this->txtName->SafeText;
		$activeRecord->BannerURL = $this->txtURL->SafeText;
		$activeRecord->BannerType = "1"; // homepage slideshow banner
		$activeRecord->Status = $this->radPublish->SelectedValue;
		$activeRecord->BannerPhoto = (strlen($hashImage)>0)?$hashImage:($activeRecord->ID>0?$activeRecord->BannerPhoto:self::NO_IMAGE);
		$activeRecord->BannerThumb = (strlen($hashThumb)>0)?$hashThumb:($activeRecord->ID>0?$activeRecord->BannerThumb:self::NO_IMAGE);
		
		return $activeRecord;
	}

	protected function btnSubmit_Clicked($sender, $param)
	{
		if ($this->IsValid)
		{
			$activeRecord = $this->bindItem();
			try
			{
				$action = ($activeRecord->ID>0 ? "update-success" : "add-success");
				$msg = $this->Application->getModule("message")->translate(($activeRecord->ID>0 ? "UPDATE_SUCCESS" : "ADD_SUCCESS"),"Banner",$activeRecord->BannerName);
				$activeRecord->save();

				if (strlen($this->Request["refUrl"])>0)
					$url = urldecode($this->Request["refUrl"])."&action=$action&msg=$msg";
				else  
					$url = $this->Service->ConstructUrl("admin.ManageBanners",array("action"=>$action, "msg"=>$msg));
				$this->Response->redirect($url);
			}
			catch(TException $e)
			{
				$this->Notice->Type = AdminNoticeType::Error;
				$this->Notice->Text = $this->Application->getModule("message")->translate(($activeRecord->ID>0 ? "UPDATE_FAILED" : "ADD_FAILED"),"Banner",$activeRecord->BannerName);
			}
		}
	}

	protected function checkImageExtension_ServerValidated($sender, $param)
	{
		if ($param->Value != '') 
			$param->IsValid = in_array(strtolower(array_pop(explode('.', $param->Value))), TPropertyValue::ensureArray($this->Application->Parameters['AVAILABLE_IMAGES']));
	}
}

?>